import org.junit.Test;
import org.springframework.util.StringUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * 精简数据库安装
 */
public class DBTidys {

    /** 精简数据库 */
    //@Test
    public void tidyDb() throws IOException {
        StringBuilder sb = new StringBuilder();
        String sourcePath = "D:\\temp\\西安公务段\\jsdz_4gdb_railway.sql";
        String targetPath = "D:\\temp\\西安公务段\\target1.sql";

        File sourceFile = new File(sourcePath);
        //RandomAccessFile targetFile =new RandomAccessFile(targetPath,"rw");
        File targetFile = new File(targetPath);

        String[] skipStrs = {"INSERT INTO `4g_mark`"
                ,"INSERT INTO `admin_operatelogs`"
                ,"INSERT INTO `admin_syslog`"
                ,"INSERT INTO `centre_datas`"
                ,"INSERT INTO `device_data_exchange`"
                ,"INSERT INTO `fileitem`"
                ,"INSERT INTO `graphensemble_device_alarm`"
                ,"INSERT INTO `graphensemble_gps_info`"
                ,"INSERT INTO `t_rec_log`"
                //,"INSERT INTO `t_recorder_copy`"
                ,"INSERT INTO `temp_item_excel1`"
                ,"INSERT INTO `temp_organization`"
                ,"INSERT INTO `temp_organization_excel`"
                ,"INSERT INTO `4g_mark`"
        };


        if (!sourceFile.exists()) throw  new FileNotFoundException();

        FileInputStream fis=new FileInputStream(sourceFile);
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));

        FileOutputStream fos = new FileOutputStream(targetFile);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

        boolean found=false;
        String tempStr=null;
        byte[] b=null;
        int i=0;
        while ((tempStr=br.readLine()) != null){

            if (tempStr.indexOf("INSERT INTO ") != -1){
                //System.out.println(tempStr);
            }

            found=false;
            for(String skip : skipStrs){
                if (!StringUtils.isEmpty(tempStr)){
                    if (tempStr.indexOf(skip) != -1){
                        found=true;
                        break;
                    }
                }
            }
            if (found){continue;}

            tempStr=tempStr+"\r\n";
            bw.write(tempStr);
            i++;
            if (i>=10000){
                System.out.println(i);
                i=0;
                //break;
            }

        }
        fis.close();
        fos.close();
        //targetFile.close();

    }

}
